package cn.wolfcode.wolf2w.business.service.impl;


import cn.wolfcode.wolf2w.business.api.domain.Answer;
import cn.wolfcode.wolf2w.business.mapper.AnswerMapper;
import cn.wolfcode.wolf2w.business.query.AnswerQuery;
import cn.wolfcode.wolf2w.business.service.IAnswerService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 回答Service业务层处理
 * 
 * @author zhuyaohua
 * @date 2024-10-01
 */
@Service
@Transactional
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Override
    public IPage<Answer> queryPage(AnswerQuery qo) {
        IPage<Answer> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public boolean insert(Answer answer) {
        Long userId = SecurityContextHolder.getUserId();


        R<UserInfo> one = remoteUserInfoService.getOne(userId, SecurityConstants.INNER);
        UserInfo data = one.getData();
        answer.setAuthorName(data.getNickname());
        answer.setHeadImgUrl(data.getHeadImgUrl());
        answer.setCreateTime(new Date());
        answer.setTitle(answer.getAnswerContent());
        answer.setDetailDoubt(answer.getAnswerContent());
        answer.setLevel(data.getLevel());

        int insert = baseMapper.insert(answer);

        return insert>0;

    }
}
